package com.synergy.stockflow.sales.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.entity.SysUser;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.sales.entity.Quotation;
import com.synergy.stockflow.sales.entity.QuoteDetails;
import com.synergy.stockflow.sales.service.QuotationService;
import com.synergy.stockflow.sales.service.QuoteDetailsService;
import com.synergy.stockflow.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/quotation")
public class QuotationController {

    @Autowired
    QuotationService quotationService;

    @Autowired
    QuoteDetailsService quoteDetailsService;

    @Autowired
    SysUserService sysUserService;
    @GetMapping
    public Result<Page<Quotation>> getQuotation(
            @RequestParam(name = "current", defaultValue = "1") int current,
            @RequestParam(name = "size", defaultValue = "5") int size,
            @RequestParam(name = "quotationId", required = false) String quotationId,
            @RequestParam(name = "quotaitionDateOfSigning", required = false) String quotaitionDateOfSigning,
            @RequestParam(name = "beginquotationSalesActually", required = false) Double beginquotationSalesActually,
            @RequestParam(name = "quotationHeadOfSales", required = false) String quotationHeadOfSales,
            @RequestParam(name = "endquotationSalesActually", required = false) Double endquotationSalesActually,
            @RequestParam(name = "customerResourcesName", required = false) String customerResourcesName,
            @RequestParam(name = "warehouseId", required = false) String warehouseId
    ) {
        Page<Quotation> page = new Page<>(current, size);
        page.setOptimizeCountSql(false);
        page.setOptimizeJoinOfCountSql(false);
        page=quotationService.getQuotation(page,current, size, quotationId, quotaitionDateOfSigning, beginquotationSalesActually,quotationHeadOfSales, endquotationSalesActually, customerResourcesName, warehouseId);
        for(Quotation q:page.getRecords()){
            LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
            q.setNickname(sysUserService.getOne(queryWrapper.eq(SysUser::getId,q.getQuotationHeadOfSales())).getNickname());
            queryWrapper=new LambdaQueryWrapper<>();
            q.setNickname1(sysUserService.getOne(queryWrapper.eq(SysUser::getId,q.getQuotaitionApprover())).getNickname());
        }
        return Result.success(page);
    }
    @GetMapping("/where")
    public Result<Page<Quotation>> getQuotation(
            @RequestParam(name = "current", defaultValue = "1") int current,
            @RequestParam(name = "size", defaultValue = "5") int size,
            @RequestParam(name = "quotationState", required = false) String quotationState
    ) {
        Page<Quotation> page = new Page<>(current, size);
        page=quotationService.selectWhere(page,current, size, quotationState);
        return Result.success(page);
    }

    @PostMapping
    public Result insert(@RequestBody Quotation quotation) {
        quotation.setQuotationTime(new Date());
        quotation.setQuotationWarehouse("WH-001");
        quotation.setQuotationState("申请中");
        quotation.setQuotationIsremove(0);
        quotationService.save(quotation);
        List<Quotation> list = quotationService.list();
        for(QuoteDetails q:quotation.getQuoteDetails()){
            q.setQuoteDetailsId("QD-"+String.valueOf(new Date().getTime()));
            q.setQuoteDetailsQuotationId(list.get(list.size()-1).getQuotationId());
            quoteDetailsService.save(q);
        }
        return Result.success(quotation);
    }

    @PutMapping
    public Result update(@RequestBody Quotation quotation) {
        quotationService.updateById(quotation);
        return Result.success(quotation);
    }

    @DeleteMapping
    public Result delete(@RequestBody List<Quotation> quotationId) {
        for(Quotation q : quotationId){
            quotationService.update(q.getQuotationId());
        }
        return Result.success(quotationId);
    }
}
